// pages/category/category.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowheight:0,
    leftmenu: 1,
    category1id:0,
    category1: [],
    category2: [],
    productlist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.Find_Category();
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ windowheight: 750 * res.windowHeight / res.windowWidth - 100 });
      }
    })
  },
  Find_Category: function () {
    var that = this;
    var data = {};
    data.act = "Find_Category";
    wx.showLoading({
      title: '数据加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      that.setData({ category1: json.data.category1, category2: json.data.category2 });
      if(json.data.category2.length==0){
        that.setData({ category1id: json.data.category1id, productlist: json.data.productlist });
      }
    });
  },
  Find_ProductByCategory1ID: function (category1id) {
    var that = this;
    var data = {};
    data.act = "Find_ProductByCategory1ID";
    data.category1id = category1id;
    app.Ajax(data, function (json) {
      that.setData({productlist: json.data.productlist });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.Find_Category();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  openSearch: function () {
    wx.navigateTo({
      url: '/pages/search/search?froms=index'
    })
  },
  openProduct: function (e) {
    var category1id = e.currentTarget.dataset.bigid;
    var category2id = e.currentTarget.dataset.smallid;
    app.Keyword = "";
    app.IsFlash = true;
    console.log(category1id + "**" + category2id);
    wx.navigateTo({
      url: '/pages/product/product?category1id=' + category1id + "&category2id=" + category2id
    })
  },
  selectMenu: function (e) {
    var id = e.currentTarget.dataset.id;
    var category1id = e.currentTarget.dataset.bigid;
    this.setData({ leftmenu: id, category1id: category1id });
    if (this.data.category2.length == 0) {
      this.Find_ProductByCategory1ID(category1id);
    }
  }
})